import numpy as np
import matplotlib.pyplot as plt


plt.rcParams['axes.unicode_minus'] = False  # 解决负号显示问题
plt.rc('font', size=10)
plt.rc('font', family='SimHei')


# 通用常数
interval = 1  # 投弹时间间隔
v_down = 3  # 云团下降速度
r_effective = 10  # 有效遮蔽半径
t_effective = 20  # 有效遮蔽时间
v_missile = 300  # 导弹速度
r_target = 7  # 目标半径
H_target = 10  # 目标高度
target = np.array([0, 200, 0])  # 目标底部圆心坐标
M1 = np.array([20000, 0, 2000])
M2 = np.array([19000, 600, 2100])
M3 = np.array([18000, -600, 1900])
FY1 = np.array([17800, 0, 1800])
FY2 = np.array([12000, 1400, 1400])
FY3 = np.array([6000, -3000, 700])
FY4 = np.array([11000, 2000, 1800])
FY5 = np.array([13000, -2000, 1300])
v_range = np.array([70, 140])  # 无人机速度范围
g = 9.8  # 重力加速度
target_point = np.array([
    [(1 / 2) ** 0.5 * r_target, (1 / 2) ** 0.5 * r_target + 200, 0],
    [-(1 / 2) ** 0.5 * r_target, (1 / 2) ** 0.5 * r_target + 200, 0],
    [(1 / 2) ** 0.5 * r_target, -(1 / 2) ** 0.5 * r_target + 200, 0],
    [-(1 / 2) ** 0.5 * r_target, -(1 / 2) ** 0.5 * r_target + 200, 0],
    [(1 / 2) ** 0.5 * r_target, (1 / 2) ** 0.5 * r_target + 200, H_target],
    [-(1 / 2) ** 0.5 * r_target, (1 / 2) ** 0.5 * r_target + 200, H_target],
    [(1 / 2) ** 0.5 * r_target, -(1 / 2) ** 0.5 * r_target + 200, H_target],
    [-(1 / 2) ** 0.5 * r_target, -(1 / 2) ** 0.5 * r_target + 200, H_target],
    [(1 / 2) ** 0.5 * r_target, (1 / 2) ** 0.5 * r_target + 200, H_target / 2],
    [-(1 / 2) ** 0.5 * r_target, (1 / 2) ** 0.5 * r_target + 200, H_target / 2],
    [(1 / 2) ** 0.5 * r_target, -(1 / 2) ** 0.5 * r_target + 200, H_target / 2],
    [-(1 / 2) ** 0.5 * r_target, -(1 / 2) ** 0.5 * r_target + 200, H_target / 2],
])

# 问题一要求
v_1 = 120
t_1 = 1.5  # 投弹时间
t_2 = 3.6  # 起爆时间


# 传入M1的初始坐标（也即可确定其飞行方向）和经过的时间，返回其在给定时间下的坐标
def get_M1_coordinate(coordinate_0, t, v=None):
    if not v:
        v = -coordinate_0 / np.linalg.norm(coordinate_0) * v_missile
    else:
        v = v / np.linalg.norm(v) * v_missile
    return coordinate_0 + v * t


def get_FY1_coordinate(coordinate_0, t, i, v=None):
    if t < t_1 + i * t_2 or t > t_1 + i * t_2 + t_effective:
        return [None, None, None]
    else:
        if v is None:
            v = -coordinate_0
        else:
            v = v - FY1
        v[2] = 0
        v = v / np.linalg.norm(v) * v_1
        delta_t = t - t_1 - t_2
        coordinate = coordinate_0 + v * (t_1 + t_2)
        coordinate[2] -= 1 / 2 * g * (t_2 ** 2)
        coordinate[2] -= delta_t * v_down
        return coordinate


def collision_detection(coordinate_missile, coordinate_bomb):
    if coordinate_bomb[0] == None:
        count_list.append(0)
        return False
    global target_point, r_effective
    count_collision = 0
    for target in target_point:
        l_1 = coordinate_bomb - target
        l_2 = coordinate_missile - target
        molecule = np.cross(l_1, l_2)
        denominator = np.linalg.norm(l_2)
        d = np.linalg.norm(molecule) / denominator
        if d < r_effective:
            missile_to_target = -coordinate_missile  # 由导弹指向目标
            missile_to_bomb = coordinate_bomb - coordinate_missile  # 由导弹指向烟幕弹
            cos = np.dot(missile_to_target, missile_to_bomb)
            if cos > 0 or np.linalg.norm(missile_to_bomb) < r_effective:  # 余弦值大于0，是锐角，或者导弹和烟幕弹之间的距离小于有效半径
                count_collision += 1
    count_list.append(count_collision)
    return count_collision


count = 0
t_list = np.arange(0, t_1 + t_2 + t_effective + 5, 0.1)  # 时间序列，终点是0
count_list = []
for t in t_list:
    print(f"t为{t}时导弹位置：{get_M1_coordinate(M1, t)}, 烟雾弹中心位置：{get_FY1_coordinate(FY1, t, 1)}")
    if collision_detection(get_M1_coordinate(M1, t), get_FY1_coordinate(FY1, t, 1)) == len(target_point):
        print("遮蔽有效")
        count += 1
print(count / 10)
# 创建图形
plt.figure(figsize=(8, 6))
# 绘制散点图，使用颜色映射
scatter = plt.scatter(t_list, count_list)
# 添加标题和轴标签
plt.title('时间与遮挡视线的个数')
plt.xlabel('时间')
plt.ylabel('遮挡')
# 显示图形
plt.show()
